// transactions.cpp
// Customizer_Pro
// Protea Customizer Pro / Business Logic for managing transactions
// Copyright (C) 2003, 2004, 2005 eXegeSys, Inc.
// Copyright (C) 2008 Bruce A. James
//
// This program is part of the Protea Project.
//
// The Protea Project is free software; you can redistribute it and/or modify it 
// under the terms of the GNU General Public License as published by the 
// Free Software Foundation, either version 3 of the License, or (at your 
// option) any later version.
//
// The Protea Project is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
// for more details.
// 
// You should have received a copy of the GNU General Public License along 
// with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// Please send correspondence to:
//
// theproteaproject@gmail.com
//

#include "pro_customizer.h"

// Transaction Subtransactions
//
//////////////////////////////////////////////////////////////////////////////

void TransactionAdd( XCBuffer* buf, XCTransactionVars* vars ) 
{
	TRANS_INFUNC( TransactionAdd );

	XCDBStatement* stmt = NULL;
	XCBuffer* temp = NULL;
	XCSQLGen* query = vars->query;

	long last_number = 0;

	//@ Determine the next available transaction number
	temp = App->CreateBuffer( FMT_TRANSACTION_DEFN );
	try {
		XCOrder ord;
		XCCompare comp;
		bool found = false;

		query->MakeSELECT( temp );
		query->AddWHERE( buf->GetField( FLD_APPL_NBR ), &comp );
		query->AddORDERBY( FLD_TRANS_NUMBER, &ord );
		stmt = vars->dbtxn->ExecuteSQL( query );
		while ( !found && !stmt->Eof() )
		{
			long value = temp->GetField( FLD_TRANS_NUMBER )->GetLong( true );
			if ( value != last_number + 1 )
			{
				// we found a blank space
				found = true;
				last_number++;
			}
			else
			{
				last_number = value;
				stmt->MoveNext();
			};
		};

		// Cleanup now
		App->CleanupObjects( 2, &temp, &stmt );

		if (!found)
			last_number++;
		buf->GetField( FLD_TRANS_NUMBER )->SetLong( last_number );

	} catch( ... ) {
		App->CleanupObjects( 2, &temp, &stmt );
		throw;
	};

	//@ Add the literal record
	query->MakeFull( SQL_STMT_INSERT, buf );
	vars->dbtxn->ExecuteSQL( query, SQL_NO_RESULT );

	TRANS_OUTFUNC();
};




void TransactionDelete( XCBuffer* buf, XCTransactionVars* vars ) 
{
	TRANS_INFUNC( TransactionDelete );

	//@ Delete the transaction record
	XCSQLGen* query = vars->query;
	query->MakeDELETE( buf );
	query->AddWHERE( 2, buf->GetField( FLD_TRANS_NUMBER ), buf->GetField( FLD_APPL_NBR ) );
	vars->dbtxn->ExecuteSQL( query, SQL_NO_RESULT );

	//@ Remove the link to the user-exit records
	XCBuffer* userexits = NULL;
	userexits = vars->myapp->CreateBuffer( FMT_USER_EXIT_DEFNS );
	try  {
		query->MakeDELETE( userexits );
		query->AddWHERE( 2, buf->GetField( FLD_TRANS_NUMBER ), buf->GetField( FLD_APPL_NBR ) );
		vars->dbtxn->ExecuteSQL( query, SQL_NO_RESULT );
	} catch( ... ) {
		userexits->Release();
		throw;
	};
	userexits->Release();

	TRANS_OUTFUNC();
};




//@ SEC 2

//@ CHA Add Transaction
//@ COD AddTransaction
//@ NUM 26
//@ CRI TRANS_CODE
//@ TAB TRANSACTION_DEFN Write
//
//@ GEN Add Transaction will add a transaction for an application
//
TRANSACTION_CODE( AddTransaction )
{
	XCBuffer* buf = NULL;

	//@ GRS Start the transaction
	START_TXN( AddTransaction );

		//@ Create Literal Defn buffer
		buf = App->CreateBuffer( FMT_TRANSACTION_DEFN );

		//@ Move screen info to transaction buffer
		buf->MoveCorresponding( txnvars.buffer, txnvars.Errors );
		txnvars.Errors->Check();

		START_DB_TXN();

			//@ SUB TransactionDefnVerify to verify that application can be added
			TransactionVerify( buf, VERIFY_ADD, &txnvars );

			//@ Check for errors
			txnvars.Errors->Check();

			//@ SUB TransactionDefnAdd to add the new application
			TransactionAdd( buf, &txnvars );

			//@ Move values back to screen buffer
			txnvars.buffer->MoveCorresponding( buf, txnvars.Errors );
			App->TransResponse( txnvars.result, txnvars.buffer, TRANS_HEADER_TYPE );

			LOG_TRANSACTION();

		END_DB_TXN();

	CLEANUP_TXN();
		App->CleanupObjects( 1, &buf );
	END_TXN();
	//@ GRE End the transaction
};






//@ CHA Delete Transaction
//@ COD DeleteTransaction
//@ NUM 27
//@ CRI TRANS_CODE
//@ TAB TRANSACTION_DEFN Write
//
//@ GEN Delete Literal will delete a transaction definition record
//
TRANSACTION_CODE( DeleteTransaction ) 
{
	XCBuffer* buf = NULL;

	//@ GRS Start the transaction
	START_TXN( DeleteTransaction );

		//@ Create transaction definition buffer
		buf = App->CreateBuffer( FMT_TRANSACTION_DEFN, false );

		//@ Move screen info to transaction buffer
		buf->MoveCorresponding( txnvars.buffer, txnvars.Errors );
		txnvars.Errors->Check();

		START_DB_TXN();

			//@ SUB TransactionDefnVerify to verify that record can be deleted
			TransactionVerify( buf, VERIFY_DELETE, &txnvars );
			txnvars.Errors->Check();

			//@ SUB TransactionDefnDelete to delete the record.
			TransactionDelete( buf, &txnvars );

			LOG_TRANSACTION();

		END_DB_TXN();

	CLEANUP_TXN();
		App->CleanupObjects( 1, &buf );
	END_TXN();
	//@ GRE End the transaction
};

